var this_date = new Date();
var this_day = this_date.getDate();
var this_month = this_date.getMonth();
var this_year = this_date.getFullYear();

var day;
var month;
var year;

var old_id = null;

var dateValeur = null;
var dateJour = null;

function calendar(date) 
{
  if ( document.getElementById("calendar") != null )
  {
	//If no parameter is passed use the current date.
	if(date == null)
		date = new Date();

	day = date.getDate();
	month = date.getMonth();
	year = date.getFullYear();

	days = new Array('Lundi',
		'Mardi',
		'Mercredi',
		'Jeudi',
		'Vendredi',
		'Samedi',
		'Dimanche');

	months = new Array('Janvier',
		'Février',
		'Mars',
		'Avril',
		'Mai',
		'Juin',
		'Juillet',
		'Aout',
		'Septembre',
		'Octobre',
		'Novembre',
		'Décembre');

	this_date_month = new Date(year, month, 1);
	next_month = new Date(year, month + 1, 1);

	//Find out when this month starts and ends.
	first_week_day = this_date_month.getDay();
	if ( first_week_day == 0 ) first_week_day = 7;
	days_in_this_month = Math.round((next_month.getTime() - this_date_month.getTime()) / (1000 * 60 * 60 * 24));

	calendar_html = '<table style="background-color:9999cc; color:ffffff; width: 190px;">';



	calendar_html += '<tr>\n\
						<td colspan="7" align="center" style="background-color:9999cc; color:000000;"> \n\
							<table width="100%"> <tr> <td> <input type="button" onclick="calendar(new Date(year,month-1,day));" value="-" width="10px"/> </td> <td width="100%"> <center>'
							+ months[month] + ' ' + year +
							'</center> </td> <td> <input type="button" onclick="calendar(new Date(year,month+1,day));" value="+" width="10px"/> \n\
						</td> </tr> </table> </td>\n\
					 </tr>';

	calendar_html += '<tr>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> L </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> M </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> M </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> J </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> V </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> S </td>\n\
						<td align="center" style="background-color:9999cc; color:000000;"> D </td>\n\
					 </tr>';

	calendar_html += '<tr>';

	//Fill the first week of the month with the appropriate number of blanks.
	//for(week_day = 1; week_day < first_week_day; week_day++)
	//{
		calendar_html += '<td colspan="'+ (first_week_day-1) +'" style="background-color:9999cc; color:000000;"> </td>';
	//}

	// colspan="'+ (first_week_day-1) +'"

	week_day = first_week_day;
	for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
	{
		week_day %= 7;

		if(week_day == 1)
			calendar_html += '</tr><tr>';

		//Do something different for the current day.
		if((day == day_counter) && (this_month == month) && (this_year == year))
			calendar_html += '<td id="day_id_'+day_counter+'" align="center" onclick="calendar_clic(new Date('+this_year+', '+this_month+', '+day_counter+'), \'day_id_'+day_counter+'\');"><b>' + day_counter + '</b></td>';
		else
			calendar_html += '<td id="day_id_'+day_counter+'" align="center" style="background-color:FFFFFF; color:000000;" onclick="calendar_clic(new Date('+this_year+', '+this_month+', '+day_counter+'), \'day_id_'+day_counter+'\');"> ' +
			day_counter + ' </td>';

		week_day++;
	}

	calendar_html += '</tr>';
	calendar_html += '</table>';

	//Display the calendar.
	//document.write(calendar_html);

	document.getElementById("calendar").innerHTML = calendar_html;
  }
}

function calendar_clic( date, id ) {

        dateJour = date.toLocaleString().substr(0, date.toLocaleString().length-8);
	if ( document.getElementById('date_jour') != null ) {
		 document.getElementById('date_jour').innerHTML = dateJour;
	}

        dateValeur = date.getTime();
        if ( document.getElementById('date_value') != null ) {
		document.getElementById('date_value').innerHTML = dateValeur;
	}

	if ( document.getElementById(id) != null ) {
		if ( old_id != null )
                    document.getElementById(old_id).style.color = "black";
                
		document.getElementById(id).style.color = "red";
		old_id = id;
	}

        // cette fonction doit être définie sur la page
        calendar_clic_page();
}